package com.th.cash.th2.main

import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.text.TextUtils
import android.view.View
import android.widget.TextView
import androidx.core.content.ContentProviderCompat.requireContext
import androidx.core.content.ContextCompat
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.th.basemodel.base.BaseVDBLazyLoadingFragment2
import com.th.basemodel.base.BaseViewModel
import com.th.basemodel.ext.bindData
import com.th.basemodel.ext.divider
import com.th.basemodel.ext.dp2px
import com.th.basemodel.ext.itemClick
import com.th.basemodel.ext.vertical
import com.th.resmodel.R
import com.th.resmodel.databinding.Fragment2Binding
import com.th.cash.storage.LocalSetting
import com.th.cash.th2.main.OrderResponse.QylNBLMBombitaFwSlPhn
import com.th.cash.util.ActivityRouter
import kotlinx.coroutines.launch

class OrderFragment : BaseVDBLazyLoadingFragment2<Fragment2Binding>() {

    private val mViewModel by viewModels<MainVm>()

    private var mOrderList: MutableList<QylNBLMBombitaFwSlPhn> = mutableListOf()

    override fun startObserve() {
        lifecycleScope.launch {
            mViewModel.uiState.collect {
                when (it) {
                    is MainState.Idle -> {

                    }

                    is MainState.OrderData -> {
                        if (it.data.bILFyuCSaletYkxkYNR == 0) {
                            mBinding.llOrderEmpty.visibility = View.VISIBLE
                            mBinding.rvOrderList.visibility = View.GONE
                        } else {
                            mBinding.llOrderEmpty.visibility = View.GONE
                            mBinding.rvOrderList.visibility = View.VISIBLE
                        }

                        mOrderList.clear()
                        if (it.data.QylNBLMBombitaFwSlPhN.isEmpty() != true) {
                            mOrderList.addAll(it.data.QylNBLMBombitaFwSlPhN)
                        }
                        mBinding.rvOrderList.adapter?.notifyDataSetChanged()
                    }

                    else -> {}
                }
            }
        }
    }

    override fun init() {
        initClick()
        initOrderAdapter()
    }

    private fun initClick() {
        mBinding.tvSubmit.setOnClickListener {
            (requireActivity() as MainActivity).showHomePage()
        }
    }

    override fun onLazyInitData() {
        mViewModel.intentChannel.trySend(
            MainIntent.GetOrders(
                OrderRequest(
                    if (LocalSetting.userAdult) {
                        "4"
                    } else {
                        "6"
                    }, "1", "99"
                )
            )
        )
    }

    override fun getVm(): BaseViewModel {
        return mViewModel
    }

    companion object {
        fun newInstance(): OrderFragment {
            return OrderFragment()
        }
    }

    private fun initOrderAdapter() {
        mBinding.rvOrderList.vertical()
            .divider(
                color = ContextCompat.getColor(requireContext(), R.color.white),
                size = dp2px(16f)
            )
            .bindData(mOrderList, R.layout.item_order) { holder, t, position ->
                holder.setText(R.id.ivOrderName, t.MhuYpFcErythritolOTRMEni)
                holder.setText(R.id.tvOrderRight, t.BJQpakjTegumentFNjjrUc)
                holder.getView<TextView>(R.id.tvOrderRight).apply {
                    text = t.BJQpakjTegumentFNjjrUc

                    try {
                        setTextColor(Color.parseColor(t.eFJwhiuUmbrellaYWQrlPe))
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                }
                holder.setText(R.id.tvOrderMoney, t.glnBxdjTerraalbaAtJPQXy)


                val tvOrderDate = holder.getView<TextView>(R.id.tvOrderDate)
                val tvOrderDateHint = holder.getView<TextView>(R.id.tvOrderDateHint)

                if (TextUtils.isEmpty(t.GbacHroCareclothNGROgQY)) {
                    tvOrderDate.visibility = View.GONE
                    tvOrderDateHint.visibility = View.GONE
                } else {
                    tvOrderDate.visibility = View.VISIBLE
                    tvOrderDateHint.visibility = View.VISIBLE

                    holder.setText(R.id.tvOrderDate, t.GbacHroCareclothNGROgQY)

                }

                val tvOrderSubmit = holder.getView<TextView>(R.id.tvOrderSubmit)

                if (TextUtils.isEmpty(t.CIyoEPBCongenitalIHfVVGV)) {
                    tvOrderSubmit.visibility = View.GONE
                } else {
                    tvOrderSubmit.visibility = View.VISIBLE

                    holder.setText(R.id.tvOrderSubmit, t.CIyoEPBCongenitalIHfVVGV)

                    try {
                        tvOrderSubmit.background = GradientDrawable().apply {
                            setColor(Color.parseColor(t.KtXpztpLiquefiableOXzPAcZ))
                            cornerRadius = resources.getDimension(R.dimen.dp_22)
                        }
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                }

                val options = RequestOptions.bitmapTransform(RoundedCorners(5))
                Glide.with(holder.itemView.context)
                    .load(t.wYgbjolRoutinierUQLXJwP)
                    .apply(options)
                    .into(holder.getView(R.id.ivOrderIcon))

            }.itemClick<QylNBLMBombitaFwSlPhn> { data, _, position ->
                ActivityRouter.next(
                    requireContext(),
                    data[position].uXutawfHidDCIXUBz
                )
            }
    }

}